package com.ardo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ardo.entity.ResponseResult;
import com.ardo.entity.mall.Shop;
import com.ardo.mapper.ShopMapper;
import com.ardo.service.ShopService;
import com.ardo.utils.MapCoordinateConversion;
import com.ardo.utils.MapDistance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Override
    public String queryShopList(String lng, String lat, String likeName) {
        String result = "";
        List<Shop> shopList = shopMapper.queryShopList(likeName);
        if(shopList != null && shopList.size()>0){
            //1.wgs84转百度坐标系
            String xy = MapCoordinateConversion.wgs84tobd09(Double.parseDouble(lng), Double.parseDouble(lat));
            String[] xyarr = xy.split(",");
            System.out.println("百度坐标 ->"+xyarr[0]+" "+xyarr[1]);
            for (int i = 0; i < shopList.size(); i++) {
                Shop shop = shopList.get(i);
                shop.setDistance(MapDistance.getDistance(xyarr[1], xyarr[0], shop.getLatitude()+"", shop.getLongitude()+""));
            }
            //2.再根据距离distance排序list

            result = JSONObject.toJSONString(ResponseResult.success(shopList));
        }


        return result;
    }
}
